package liu.mp3player;

import java.util.*;

import liu.download.SdcardWrite;
import liu.model.Mp3Info;

import android.app.*;
import android.content.Intent;
import android.os.*;
import android.view.View;
import android.widget.*;

public class LocalMp3ListActivity extends ListActivity{
	private List<Mp3Info> localMp3Infos=null;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mp3info_listview);
    }
	
	private void updateListView(){
		SdcardWrite sdcardRead=new SdcardWrite();
		localMp3Infos=sdcardRead.getLocalMp3Info("Mp3/");
		ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
		for (Iterator iterator = localMp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info localmp3Info=(Mp3Info) iterator.next();
			HashMap<String,String> localmp3=new HashMap<String,String>();
			localmp3.put("mp3_name", localmp3Info.getMp3Name());
			localmp3.put("mp3_size", localmp3Info.getMp3Size());
			list.add(localmp3);
		}
		SimpleAdapter simple=new SimpleAdapter
				(this, list, R.layout.mp3info_item, 
						new String[] {"mp3_name","mp3_size"},
						new int[] {R.id.mp3_name,R.id.mp3_size});	
		setListAdapter(simple);
	}

	@Override
	protected void onResume() {
		updateListView();
		super.onResume();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {		
		if(localMp3Infos!=null){
			Mp3Info localmp3Info=localMp3Infos.get(position);
			Intent intent=new Intent();
			intent.putExtra("localmp3Info", localmp3Info);
			intent.setClass(this, PlayerActivity.class);
			startActivity(intent);
		}		
	}
	
	
}
